package com.vhr.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vhr.VO.DimissionAdminVO;
import com.vhr.VO.DimissionVO;
import com.vhr.entities.Dimission;
import com.vhr.entities.Employee;
import com.vhr.entities.MyPageInfo;
import com.vhr.entities.Result;
import com.vhr.service.IDimissionService;
import com.vhr.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(description = "离职模块")
//@CrossOrigin
@CrossOrigin(allowCredentials="true",allowedHeaders="*")
public class DimissionController {

    @Autowired
    private IDimissionService iDimissionService;

    @Autowired
    private IEmployeeService iEmployeeService;

    //查询所有离职记录
    //http://localhost:8080/dimission/list
    @ApiOperation("管理员：查询所有离职记录")
    @GetMapping("/api/dimission/list")
    public Result dimissionList(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                               @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){

        PageHelper.startPage(pageNum,pageSize);
        List<Dimission> list = iDimissionService.selectAll();
        PageInfo<Dimission> pageInfo = new PageInfo<>(list);

        if (pageInfo.getList()==null){
            return Result.fail();
        }

        List<DimissionAdminVO> VOlist = iDimissionService.AdminSelectAll();
        ArrayList<DimissionAdminVO> voList = new ArrayList<>();

        for(int i=0;i<list.size();i++){
            Dimission s = list.get(i);
            DimissionAdminVO vo = new DimissionAdminVO();
            for(int n=0;n<VOlist.size();n++){
                if(list.get(i).getDimissionId()==VOlist.get(n).getDimissionId()){
                    vo.setDeptName(VOlist.get(n).getDeptName());
                    vo.setEmpNum(VOlist.get(n).getEmpNum());
                    vo.setEmpName(VOlist.get(n).getEmpName());
                    vo.setDimissionStatus(VOlist.get(n).getDimissionStatus());
                    vo.setDimissionReason(VOlist.get(n).getDimissionReason());
                    vo.setDimissionTime(VOlist.get(n).getDimissionTime());
                    vo.setDimissionId(VOlist.get(i).getDimissionId());
                    voList.add(vo);
                }
            }
        }
        MyPageInfo<DimissionAdminVO> myPageInfo = new MyPageInfo<>();
        myPageInfo.setList(voList);
        myPageInfo.setPageNum(pageNum);
        myPageInfo.setPageSize(pageSize);
        myPageInfo.setRecordCount((int)pageInfo.getTotal());
        myPageInfo.setTotalPage(pageInfo.getPages());
        return Result.success().add("dimissionList",myPageInfo);

    }

    //根据id查询离职记录
    @ApiOperation("用户：根据id查询离职记录")
    @CrossOrigin//跨域请求操作
    @GetMapping("/dimission/dimissionInfo")
    public Result dimissionInfo(@RequestParam("id")Integer id,
                               @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                               @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
        Employee employee = new Employee();
        employee.setEmpId(id);

        PageHelper.startPage(pageNum,pageSize);
        List<Dimission> list = iDimissionService.selectByEmployee(employee);
        PageInfo<Dimission> pageInfo = new PageInfo<>(list);
        if(pageInfo.getList()==null){
            return Result.fail();
        }
        ArrayList<DimissionVO> dimissionVOList = new ArrayList<>();
        Employee e = new Employee();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        for (Dimission d : pageInfo.getList()) {
            DimissionVO vo = new DimissionVO();
            BeanUtils.copyProperties(d,vo);
            e.setEmpId(d.getEmpId());
            vo.setJudgeName(iEmployeeService.selectEmployeeByPrimaryKey(e).getEmpName());
            vo.setSubmitTime(format.format(d.getSubmitTime()));
            dimissionVOList.add(vo);
        }
        MyPageInfo<DimissionVO> myPageInfo = new MyPageInfo<>();
        myPageInfo.setList(dimissionVOList);
        myPageInfo.setPageNum(pageNum);
        myPageInfo.setPageSize(pageSize);
        myPageInfo.setRecordCount((int)pageInfo.getTotal());
        myPageInfo.setTotalPage(pageInfo.getPages());
        return Result.success().add("dimissionList",myPageInfo);
    }

    //插入离职信息
    @ApiOperation("用户：插入离职信息")
    @PostMapping("/dimission/insert")
    public Result dimissionInsert(@RequestBody Dimission dimission){
        System.out.println("-------------------"+dimission.toString()+"-----------");
        //dimission.setDimissionStatus(0);
        dimission.setDimissionStatus(0);
        dimission.setJudgeId(1);
        int result = iDimissionService.addDimission(dimission);
        //插入失败
        if(result==0){
            return Result.fail();
        }
        //插入成功
        return Result.success();
    }

    @ApiOperation("管理员：查看已操作的离职申请,1为已同意，2为已拒绝")
    @GetMapping("/api/dimission/operated")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name="pageNum",required = false,value = "第几页",defaultValue = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query",name="pageSize",required = false,value = "一页有几条数据",defaultValue = "10",dataType = "Integer")
    })
    public Result dimissionOperated(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                   @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){

        PageHelper.startPage(pageNum,pageSize);
        List<DimissionAdminVO> list = iDimissionService.selectOperated();
        PageInfo<DimissionAdminVO> pageInfo = new PageInfo<>(list);

        MyPageInfo<DimissionAdminVO> myPageInfo = new MyPageInfo<>();
        myPageInfo.setTotalPage(pageInfo.getPages());
        myPageInfo.setRecordCount((int) pageInfo.getTotal());
        myPageInfo.setList(pageInfo.getList());
        myPageInfo.setPageSize(pageInfo.getPageSize());
        myPageInfo.setPageNum(pageInfo.getPageNum());
        return Result.success().add("list",myPageInfo);
    }


    /**/
    @ApiOperation("管理员：查看已操作的离职申请,0为未审批")
    @GetMapping("/api/dimission/noOperated")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name="pageNum",required = false,value = "第几页",defaultValue = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query",name="pageSize",required = false,value = "一页有几条数据",defaultValue = "10",dataType = "Integer")
    })
    public Result dimissionNoOperated(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                     @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){

        PageHelper.startPage(pageNum,pageSize);
        List<DimissionAdminVO> list = iDimissionService.selectNoOperated();
        PageInfo<DimissionAdminVO> pageInfo = new PageInfo<>(list);

        MyPageInfo<DimissionAdminVO> myPageInfo = new MyPageInfo<>();
        myPageInfo.setTotalPage(pageInfo.getPages());
        myPageInfo.setRecordCount((int) pageInfo.getTotal());
        myPageInfo.setList(pageInfo.getList());
        myPageInfo.setPageSize(pageInfo.getPageSize());
        myPageInfo.setPageNum(pageInfo.getPageNum());
        return Result.success().add("list",myPageInfo);
    }


    //管理员：同意离职申请操作
    @ApiOperation("管理员：同意离职申请，需要传入离职申请参数的参数: dimissionId 和审批人ID: judgeId")
    @PutMapping("/api/dimission/agree")
    //与前端整合需要改回@RequestBody???
    public Result agree(Integer dimissionId,Integer judgeId){
        Dimission dimission = new Dimission();
        dimission.setDimissionId(dimissionId);
        Employee admin = new Employee();
        admin.setEmpId(judgeId);
        iDimissionService.judegDimission(dimission,admin,1);
        return Result.success();
    }

    //管理员：不同意离职申请操作
    @ApiOperation("管理员：拒绝离职申请，需要传入离职申请参数的参数: dimissionId 和审批人ID: judgeId")
    @PutMapping("/api/dimission/disagree")
    //与前端整合需要改回@RequestBody???
    public Result disagree(Integer dimissionId,Integer judgeId){
        Dimission dimission = new Dimission();
        dimission.setDimissionId(dimissionId);
        Employee admin = new Employee();
        admin.setEmpId(judgeId);
        iDimissionService.judegDimission(dimission,admin,2);
        return Result.success();
    }

    @ApiOperation("管理员：根据离职id,删除离职申请")
    @DeleteMapping("/api/dimission/deleteDimission")
    public Result deleteDimission(Integer id){
        Dimission dimission = new Dimission();
        dimission.setDimissionId(id);
        int result = iDimissionService.deleteByPrimary(dimission);
        if(result==1){
            return Result.success();
        }
        return Result.fail();
    }

    //管理员:条件模糊搜索查询离职申请记录
    @GetMapping("/api/dimission/listDimissionByCondition")
    @ResponseBody
    @ApiOperation("管理员:条件模糊搜索查询离职申请记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId", paramType = "query", value = "部门id", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "empName", paramType = "query", value = "员工姓名", required = false, dataType = "String"),
            @ApiImplicitParam(name = "dimissionStatus", paramType = "query", value = "审批状态", required = false, dataType = "Integer"),
    })
    public Result listLeaveByCondition(@RequestParam(value = "deptId", defaultValue = "0")Integer deptId,
                                       @RequestParam(value = "empName",defaultValue = "%%")String empName,
                                       @RequestParam(value = "dimissionStatus", defaultValue = "-1")Integer dimissionStatus,
                                       @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                       @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){

        PageHelper.startPage(pageNum,pageSize);
        List<DimissionAdminVO> list = iDimissionService.listDimissionByCondition(deptId, empName, dimissionStatus);
        PageInfo<DimissionAdminVO> pageInfo = new PageInfo<>(list);

        for(int i=0;i<list.size();i++){
            String time = list.get(i).getDimissionTime().substring(0,10);
            list.get(i).setDimissionTime(time);
        }

        MyPageInfo<DimissionAdminVO> myPageInfo = new MyPageInfo<>();
        myPageInfo.setTotalPage(pageInfo.getPages());
        myPageInfo.setRecordCount((int) pageInfo.getTotal());
        myPageInfo.setList(pageInfo.getList());
        myPageInfo.setPageSize(pageInfo.getPageSize());
        myPageInfo.setPageNum(pageInfo.getPageNum());
        return Result.success().add("list",myPageInfo);
    }
}
